package com.cfuture08.eweb4j.mvc.config;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import com.cfuture08.eweb4j.cache.MVCConfigBeanCache;
import com.cfuture08.eweb4j.cache.SingleBeanCache;
import com.cfuture08.eweb4j.config.CheckConfigBean;
import com.cfuture08.eweb4j.config.ConfigConstant;
import com.cfuture08.eweb4j.config.bean.ConfigBean;
import com.cfuture08.eweb4j.config.bean.MVCXmlFiles;
import com.cfuture08.eweb4j.mvc.config.bean.MVCConfigBean;
import com.cfuture08.util.BeanXMLUtil;
import com.cfuture08.util.FileUtil;
import com.cfuture08.util.LogUtil;
import com.cfuture08.util.StringUtil;

public class MVCConfig {
	public synchronized static String check() {
		String error = null;
		ConfigBean cb = (ConfigBean) SingleBeanCache
				.get(ConfigConstant.CONFIGBEAN_ID);
		
		if (cb != null) {
			for (MVCXmlFiles xml : cb.getMvc().getXmlFiles()) {
				String filePath = xml.getPath();
				File configFile = new File(StringUtil.uriDecoding(FileUtil
						.getRootDir(MVCConfig.class)
						+ filePath));
				try {
					List<MVCConfigBean> mvcList = BeanXMLUtil.getBeanXMLReader(
							configFile).read();
					if (mvcList == null || mvcList.isEmpty()) {
						error = "MVCConfig.class : 读取不了任何配置文件信息！已经重写了配置文件，请重新填写完整，然后启动框架。";
					} else {
						for (Iterator<MVCConfigBean> it = mvcList.iterator(); it
								.hasNext();) {
							MVCConfigBean mvc = it.next();
							String error1 = CheckConfigBean
									.check(mvc, filePath);
							if (error1 != null) {
								if (error != null) {
									error += error1;
								} else {
									error = error1;
								}
							}
							String error2 = CheckConfigBean.checkMVC(mvc
									.getResult(), mvc.getName(), filePath);
							if (error2 != null) {
								if (error != null) {
									error += error2;
								} else {
									error = error2;
								}
							}

							String error3 = CheckConfigBean.checkFormBean(mvc
									.getFormBean(), mvc.getName(), filePath);
							if (error3 != null) {
								if (error != null) {
									error += error3;
								} else {
									error = error3;
								}
							}

							String error4 = CheckConfigBean.checkValidator(mvc
									.getValidator(), mvc.getName(), filePath);
							if (error4 != null) {
								if (error != null) {
									error += error4;
								} else {
									error = error4;
								}
							}
						}

						if (error == null) {
							for (Iterator<MVCConfigBean> it = mvcList
									.iterator(); it.hasNext();) {
								MVCConfigBean mvc = it.next();
								if (!"".equals(mvc.getClazz())) {
									if (!"".equals(mvc.getName())) {
										MVCConfigBeanCache.add(mvc.getName(),
												mvc);
									}
								}
							}
							// ------log-------
							String debug = ((ConfigBean) SingleBeanCache
									.get(ConfigConstant.CONFIGBEAN_ID))
									.getMvc().getDebug();
							if ("true".equals(debug) || "1".equals(debug)) {
								LogUtil.getLogger().debug(
										"MVC组件启动配置文件验证通过，已将读取到的配置信息存入缓存");
							}
							// ------log-------
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
					try {
						// 保存为备份文件
						File tf = new File(configFile.getAbsolutePath()
								+ ".back"
								+ StringUtil.getNowTime("_MMddHHmmss"));
						FileUtil.copy(configFile, tf);
						BeanXMLUtil.getBeanXMLWriter(configFile,
								MVCConfigBeanCreator.getMVCBean()).write();
						error = "MVCConfig.class : 配置文件读取错误，已重写。异常捕获：";
						LogUtil.getLogger().error(error, e);
					} catch (Exception e1) {
						e1.printStackTrace();
						error = "MVCConfig.class : 无法重写配置文件！异常捕获";
						LogUtil.getLogger().error(error, e1);
					}
				}
			}
			if (error != null) {
				MVCConfigBeanCache.clear();
			}
		}
		return error;
	}
}
